package fm.dongman.animefm.view.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.modellib.model.ComicModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 轮播图设置器
 * 第三方轮播图不支持 Kotlin 使用该类辅助
 * Created by shize on 2017/9/28.
 */

public class BannerHelper {
    private BannerHelper() {

    }

    /**
     * 获取设置后的 Banner
     *
     * @param banner 轮播图
     * @param urls   图片路径
     * @return 设置后的轮播图
     */
    public static ConvenientBanner getBannerImages(ConvenientBanner banner, List<String> urls) {
        if (urls == null)
            return banner;

        banner.setPages(new CBViewHolderCreator<BannerImageHolder>() {
            @Override
            public BannerImageHolder createHolder() {
                return new BannerImageHolder();
            }
        }, urls).setPointViewVisible(true);
        return banner;
    }

    /**
     * 获取设置布局后的 Banner
     *
     * @param banner 轮播图
     * @param models 动漫模型
     * @return 设置后的 Banner
     */
    public static ConvenientBanner getBannerLayouts(final ConvenientBanner banner, List<IModel.IComicModel> models) {
        if (models == null)
            return banner;

        banner.setPages(new CBViewHolderCreator<BannerLayoutHolder>() {
            @Override
            public BannerLayoutHolder createHolder() {
                return new BannerLayoutHolder();
            }
        }, models).setPointViewVisible(true);
        return banner;
    }

    /**
     * 轮播图图片 Holder
     */
    private static class BannerImageHolder implements Holder<String> {
        private ImageView imageView; // 要显示的图片

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            // 加载图片
            Glide.with(context).load(data).placeholder(R.mipmap.maimeng).into(imageView);
        }
    }

    /**
     * 轮播图布局 Holder
     */
    private static class BannerLayoutHolder implements Holder<ComicModel> {
        private BannerHolderView mItemViewHolder = null; // 每个 item 布局

        @SuppressLint("InflateParams")
        @Override
        public View createView(Context context) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_comic_latest_anime, null);
            mItemViewHolder = new BannerHolderView(itemView);
            return itemView;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void UpdateUI(final Context context, int position, final ComicModel comic) {
            // 加载图片
            Glide.with(context).load(comic.getImgUrl()).placeholder(R.mipmap.maimeng).into(mItemViewHolder.mIvImage);
            mItemViewHolder.mTvTitle.setText(comic.getTitle());
            mItemViewHolder.mTvGenre.setText(comic.getGenre());
            mItemViewHolder.mTvDirectory.setText(comic.getDirector());
            mItemViewHolder.mTvState.setText(comic.getInsertTime() + " 更新至第" + comic.getEpisodes() + "话");
            mItemViewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    跳转到动漫详情
                    if (context instanceof Activity) {
                        Intent intent = new Intent(context, SingleActivity.class);
                        intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_ANIME_INFO());
                        intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(),comic.getId());
                        ActivityHelper.INSTANCE.startActivity((Activity) context, intent);
                    } else {
                        throw new RuntimeException(context.toString() + "必须是Activity!");
                    }
                }
            });

        }

        private class BannerHolderView {
            ImageView mIvImage = null;
            TextView mTvTitle = null;
            TextView mTvGenre = null;
            TextView mTvDirectory = null;
            TextView mTvState = null;
            View view;

            BannerHolderView(View itemView) {
                mIvImage = (ImageView) itemView.findViewById(R.id.roundedImageView);
                mTvTitle = (TextView) itemView.findViewById(R.id.mTvComicLatestTitle);
                mTvGenre = (TextView) itemView.findViewById(R.id.mTvComicLatestType);
                mTvDirectory = (TextView) itemView.findViewById(R.id.mTvComicLatestDirector);
                mTvState = (TextView) itemView.findViewById(R.id.mTvComicLatestUpdateInfo);
                view=itemView;
            }
        }
    }
}
